package com.giao.controller;

import com.giao.pojo.FactoryC;
import com.giao.service.FactoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/factoryc")
public class FactoryController {
    @Resource
    private FactoryService factoryService;

    //查询全部
    @RequestMapping("/findAll")
    private String findAll(Model model) {
        List<FactoryC> factoryCS = factoryService.findAllFactory();
        model.addAttribute("list", factoryCS);
        return "basicinfo/factory/jFactoryList";
    }

    //删除
    @RequestMapping("/deleteFactory/{factory_id}")
    public String delete(@PathVariable("factory_id") int id) {
        factoryService.deleteFactory(id);
        return "redirect:/factoryc/findAll";
    }

    //跳转到添加页面
    @RequestMapping("/jFactoryCreate")
    public String toInsert() {
        return "basicinfo/factory/jFactoryCreate";
    }

    //添加的请求
    @RequestMapping("/insertFactory")
    public String insert(FactoryC factoryC) {
        factoryService.insertFactory(factoryC);
        return "redirect:/factoryc/findAll";
    }

    //跳转到修改页面
    @RequestMapping("/jFactoryUpdate")
    public String toUpdate(int id, Model model) {
        FactoryC factoryC = factoryService.FactoryById(id);
        model.addAttribute("updateF", factoryC);
        return "basicinfo/factory/jFactoryUpdate";
    }

    //修改
    @RequestMapping("/updateFactory")
    public String update(FactoryC factoryC) {
        factoryService.updateFactory(factoryC);
        return "redirect:/factoryc/findAll";
    }

    @RequestMapping("/start")
    public String start(Integer id) {
        factoryService.updatestartok(id);
        return "redirect:/factoryc/findAll";
    }

    @RequestMapping("/stop")
    public String stop(Integer id) {
        factoryService.updatestartno(id);
        return "redirect:/factoryc/findAll";
    }

    /*批量删除*/
    @RequestMapping("delete.action")
    public String delete(Integer[] factory_id) {
        for (int i = 0; i < factory_id.length; i++) {
            factoryService.deleteFactoryC(factory_id[i]);
        }
        return "redirect:/factoryc/findAll";
    }
}
